package authentication.ejb.usermanagement;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;



import org.apache.commons.codec.digest.DigestUtils;

import dtos.UserDTO;



/**
 * Entity implementation class for Entity: UserEntity
 *
 */
@Entity
@Table(name="USER")
@NamedQueries({
	@NamedQuery(name=User.FIND_ALL,
				query="SELECT u FROM User u ")
})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static final String FIND_ALL = "User.findAll";
	   
	@Id
	private String username;
	
    private String name;
      
    private String surname;
	
	private String password; //sha-256 + hex
	
	@ElementCollection(targetClass = Group.class)
    @CollectionTable(name = "USER_GROUP",
                    joinColumns = @JoinColumn(name = "userid"))
    @Enumerated(EnumType.STRING)
    @Column(name="groupid")
    private List<Group> groups;
	
	public User() {
		super();
	}
	
	public User(UserDTO user){
         
        this.username        = user.getUsername();
        this.name    = user.getName();
        this.surname     = user.getSurname();        
        this.password     = DigestUtils.sha256Hex(user.getPassword() );
       
    }
	 
	public UserDTO getUserDTO(){
		UserDTO u = new UserDTO();
		u.setName(this.name);
		u.setPassword(this.password);
		u.setSurname(this.surname);
		u.setUsername(this.username);
		return u;
	}
	
	public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getSurname() {
        return surname;
    }
 
    public void setSurname(String surname) {
        this.surname = surname;
    }
 
    public String getUsername() {
        return username;
    }
  
    public void setUsername(String username) {
        this.username = username;
    }
  
    /**
     * @return the password in SHA256 HEX representation
     */
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public List<Group> getGroups() {
        return groups;
    }
 
    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }
 
    @Override
    public String toString() {
        return "User [username=" + username + ", name=" + name
                + ", surname=" + surname + ", password=" + password
                + ", groups=" + groups + "]";
    }
    
    
    public boolean isEmployee(){
    	String roles = this.getName();
    	if (roles.contains("EMPLOYEES")){
    		return true;
    	}
    	
    	return false;
    }
}
